import java.io.*;
import java.net.*;
import java.util.regex.*;
public class SearchClass{
	
	public String[] search(String address) throws IOException{
		
		//i am catching all exceptions but i added the throw IOException above because i did not 
		//want to put a try catch around the fileReader.readline while loop
		int numberOfLinesRequired=3; //this will help is obtaining only the last three post
		String  listing,
				listingAddress,
				listingTitle,
				fileLine,
		        numberForListing,
		        inputline;
		String results[]=new String[3];
		int resultsCounter=0;
		URLConnection connection = null;
		BufferedReader fileReader = null;
		BufferedWriter fileWriter=null;
		BufferedReader in=null;
		
		try{
		URL url= new URL(address);
		connection = url.openConnection();
		connection.setDoOutput(true);
		OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
		out.close();
		}
		catch(MalformedURLException e){
			
		}
		catch(IOException e){
			
		}
		try{
			
		in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
		fileWriter = new BufferedWriter(new FileWriter("/home/kes/Desktop/page"));
		fileReader = new BufferedReader(new FileReader("/home/kes/Desktop/page"));
		
		while((inputline=in.readLine())!=null){
		   fileWriter.write(inputline);
		   fileWriter.newLine();
		}
		fileWriter.close();
		in.close();
		}
		catch(IOException e){
			
		}
	   Pattern pattern0 =Pattern.compile("(\\s+)+<p class=.row.>");
	   Pattern pattern1= Pattern.compile("<p>+(\\s+)+(Jan)|(Feb)|(Mar)|(Apr)|(May)|(Jun)|(Jul)|(Aug)|(Sep)|(Oct)|(Nov)|(Dec)+(\\s+)+[0-3]+[0-9]+(\\s+).+href=");
	   while((fileLine=fileReader.readLine())!=null&&numberOfLinesRequired>0){
		    Matcher match0 =pattern0.matcher(fileLine);
		   	Matcher match1=pattern1.matcher(fileLine);
		   	
		    if(match0.find()){
		    	try{
		   		fileReader.readLine();
		   		listing=fileReader.readLine();
		   		listingAddress=listing.substring(listing.indexOf("\"")+1,listing.indexOf("\">")-1);
		   		results[resultsCounter++]=listing.substring(listing.indexOf(">")+1,listing.indexOf(" -</a>"));
		   		numberOfLinesRequired--;
		    	}
		    	catch(Exception e){
		    		
		    	}
		   	}
		    if(match1.find()){
		    	int temp1;
		    	try{
		    	temp1=fileLine.indexOf("html\">");
		    	results[resultsCounter++]=fileLine.substring(temp1+6, fileLine.indexOf("</a>", temp1));
		    	temp1=fileLine.indexOf("html\">", temp1+6);
		    	results[resultsCounter++]=fileLine.substring(temp1+6,fileLine.indexOf("</a>", temp1));
		    	temp1=fileLine.indexOf("html\">", temp1+6);
		    	results[resultsCounter++]=fileLine.substring(temp1+6,fileLine.indexOf("</a>", temp1));
		    	}
		    	catch(Exception e){
		    		
		    	}
		    	
		    	numberOfLinesRequired=0;
		    }
	   }
	   
	   fileReader.close();
	   return results;
	}
	}